package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import service.PlayerBusiness;
import service.ServiceFactory;
import util.GetPath;

@Controller
public class OtherTransfer {

	private PlayerBusiness playerBusiness = ServiceFactory.getPlayBusiness();

	@ResponseBody
	@RequestMapping(value = "/getSong/{songName}", method = {
			RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public String getSong(@PathVariable("songName") String songName) {
		String json = playerBusiness.getSong(songName);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/reco", method = { RequestMethod.POST },produces = "application/json;charset=UTF-8")
	public String reco(MultipartFile recoFile) {
		if (recoFile != null) {
			Properties properties = new Properties();
			InputStream inputStream = OtherTransfer.class.getClassLoader().getResourceAsStream("path.properties");
			try {
				properties.load(inputStream);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			String path = properties.getProperty("rawPath");
			String filename = recoFile.getOriginalFilename();
			System.out.println(path + filename);
			File file = new File(path + filename);
			try {
				recoFile.transferTo(file);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			 String temp = playerBusiness.readSource();
			 System.out.println(temp+"server");
			 return temp;
		} else {
			return "{\"type\":\"invalidInfo\",\"msg\":\"fileUpload failed\"}";
		}
	}

	@RequestMapping(value = "/download", method = { RequestMethod.GET })
	public void download(HttpServletResponse response) {
		FileInputStream fileInputStream = null;
		OutputStream outputStream = null;
		GetPath getPath = new GetPath();
		String apkPath = getPath.getApkPath();
		String[] temp = apkPath.split("/");
		String filename = temp[temp.length - 1];
		response.setHeader("content-disposition", "attachment;filename=" + filename);
		File file = new File(apkPath);
		try {
			fileInputStream = new FileInputStream(file);
			outputStream = response.getOutputStream();
			int len = 0;
			byte[] buffer = new byte[512];
			while ((len = fileInputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, len);
				outputStream.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
